package expressions.slogo;

import java.util.ArrayList;
import java.util.List;
import parsing.Token;
import exceptions.EvaluationException;
import expressions.Expression;
import expressions.IContext;
import expressions.Prefix;

/**
 * Sets active turtles (turtles that are listening to commands).
 */
@Token(regex = "tell")
public class Tell extends Prefix<Double>
{

    public Tell()
    {
        super(1);
    }

    @Override
    public Double evaluate(IContext<Double> context)
    {
        Expression<Double> child = children.get(0);

        if (child instanceof LeftBracket && context instanceof ITurtleContext)
        {
            List<Double> grandChildren = ((LeftBracket) child)
                    .getEvaluatedChildren(context);

            List<Integer> turtleIDs = new ArrayList<Integer>();

            for (Double gc : grandChildren)
                turtleIDs.add((int) (double) gc);

            ((ITurtleContext) context).setActiveTurtles(turtleIDs);

            return grandChildren.get(0);
        }
        else
        {
            throw new EvaluationException();
        }

    }

}
